﻿using BlogAug22.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BlogAug22.Controllers
{
    public class HomeController : Controller
    {
        List<MyPost> myposts = Singleton.Instance.MyPosts;
        List<MyComment> mycomments = Singleton.Instance.MyComments;
        public ActionResult Index()
        {
            IndexVM holder = new IndexVM();
            holder.MyPosts = myposts;
            return View(holder);
        }
        public ActionResult Details(int id) {
            IndexVM holder = new IndexVM();
            holder.MyPosts.Add(myposts.Where(x => x.ID == id).FirstOrDefault());
            holder.Comments = mycomments.Where(x => x.PostID == id).ToList();

            return View(holder);
        }
        [HttpGet]
        public ActionResult AddPost()
        {
          return View(); 
        }
        [HttpPost]
        public ActionResult AddPost(string name, string post, string blog)
        {
            MyPost mypost = new MyPost();
            mypost.Name = name;
            mypost.Post = post;
            mypost.Blog = blog;
            mypost.ID = MyPost.NextID++;
            myposts.Add(mypost);

            return RedirectToAction("Index");
        }
        [HttpGet]
        public ActionResult AddComment(int id)
        {
            var mypost = myposts.Where(x => x.ID == id).FirstOrDefault();
            return View(mypost);
        }
        [HttpPost]
        public ActionResult AddComment(string name, string comment, int postid)
        {
            MyComment mycomment = new MyComment();
            mycomment.Name = name;
            mycomment.Comment = comment;
            mycomment.CommentID = MyComment.NextCommentID++;
            mycomment.PostID = postid;
            mycomments.Add(mycomment);

            return RedirectToAction("Details", new { id = postid });
        }
        public ActionResult Edit(int id)
        {
            MyPost mypost = myposts.Where(x => x.ID == id).FirstOrDefault();
            return View(mypost);
        }
        [HttpPost]
        public ActionResult Edit(MyPost mypost)
        {
            var m = myposts.Where(x => x.ID == mypost.ID).FirstOrDefault();
            if (m.GetType().ToString() == "BlogAug22.Models.MyPost")
            {
                MyPost oldMyPost = (MyPost)m;
                oldMyPost.Name = mypost.Name;
                oldMyPost.Post = mypost.Post;
                oldMyPost.Blog = mypost.Blog;
            }
            return RedirectToAction("Details", new { id = mypost.ID });
        }
        public ActionResult Delete(int id)
        {
            var target = myposts.Where(x => x.ID == id).FirstOrDefault();
            myposts.Remove(target);

            return RedirectToAction("Index");
        }
        public ActionResult DeleteComment(int id)
        {
            var target = mycomments.Where(x => x.CommentID == id).FirstOrDefault();
            mycomments.Remove(target);

            return RedirectToAction("Details", new { id = id });
        }
        public ActionResult About()
        {
            ViewBag.Message = "Greatest Blog Ever";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Greatest Blog Contact Info";

            return View();
        }
    }
}